package uk.ac.aber.paws.client.idlelogger.gui;

import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 8th August 2011
 * 
 * SwingLink.java
 * 
 * Displays a hyperlink in a Java container window 
 *    
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class SwingLink extends JLabel {     
	
	private static final long serialVersionUID = 8273875024682878518L;     
	private String text;    
	private URI uri;      
	
	public SwingLink(String text, URI uri) {         
		super();         
		setup(text,uri);     
	}      
	public SwingLink(String text, String uri) {         
		super();         
		URI oURI;         
		
		try {             
			oURI = new URI(uri);         
		}
		catch (URISyntaxException e) {            
	
			throw new RuntimeException(e);         
		}         
		
		setup(text,oURI);    
	}     
	
	public void setup(String t, URI u){        
		text = t;        
		uri = u;        
		setText(text);         
		setToolTipText(uri.toString());         
		
		addMouseListener(new MouseAdapter() {                 
			public void mouseClicked(MouseEvent e) {                     
				open(uri);                
			}  
			
			public void mouseEntered(MouseEvent e) {                     
				setText(text,false);                
			}                
			
			public void mouseExited(MouseEvent e) {                   
				setText(text,true);                
			}        
		});    
	}      
	
	@Override     
	public void setText(String text) {        
		setText(text,true);    
	}      
	public void setText(String text, boolean ul) {        
		String link = ul ? "<u>"+text+"</u>" : text;        
		super.setText("<html><span style=\"color: #000099;\">"+link+"</span></html>");  
		this.text = text;    
	}      
	public String getRawText(){         
		return text;     
	}      
	private static void open(URI uri) {   
		
		if (Desktop.isDesktopSupported()) {
			
			Desktop desktop = Desktop.getDesktop();                 
			
			try {                        
				desktop.browse(uri);                
			} 
			catch (IOException e) {                    
				JOptionPane.showMessageDialog(null,"Failed to launch the link, " + 
						"your computer is likely misconfigured.",   
						"Cannot Launch Link",JOptionPane.WARNING_MESSAGE);            
			}         
		} 
		else {             
			JOptionPane.showMessageDialog(null,"Java is not able to launch links on your computer.","Cannot Launch Link",JOptionPane.WARNING_MESSAGE);       
		}     
	}  
	
}